<?php
/*
 * Author: Quan Van Sinh
 * Email : sinhvnb@vietnambiz.com
 * Mobile: 0972405165
 */
//category function
function getCatPropertyIdToArray($parent_id = 0,$trees=null) {
    global $db;
    if(!$trees) $trees = array();
    $rsCat = $db->query("SELECT cat_id FROM cat_property WHERE  cat_parent = ".intval($parent_id));
    if($rsCat) {
        while($rs = $db->fetch_array($rsCat)) {
            $trees[] = $rs['cat_id'];
            $trees = getCatPropertyIdToArray($rs['cat_id'],$trees);
        }
    }
    return $trees;
}

function getCatIdByPropertyTitleLink($property_links='') {
    global $db;
    if($property_links) {
        $rs = $db->query_first("SELECT cat_id FROM property WHERE title_link = '".mysql_real_escape_string($property_links)."'");
        if($rs['cat_id']) {
            return $rs['cat_id'];
        }else {
            return false;
        }
    }else {
        return false;
    }
}

function getCategoryType($type='sell') {
    global $db,$CONFIG;
    if(isset($_REQUEST['type'])) {
        $type = $_REQUEST['type'];
    }
    if($type=='sell') {
        $cat_parent = 1;
    }elseif($type=='rent') {
        $cat_parent = 2;
    }else {
        $cat_parent = 1;
    }
    $rsCatProperty = $db->query("SELECT cat_id,cat_title FROM cat_property WHERE cat_parent=$cat_parent ORDER BY cat_order");
    $option = '<option value="0">Select category...</option>';
    while($rs=$db->fetch_array($rsCatProperty)) {
        $option .= '<option value="'.$rs['cat_id'].'">'.$rs['cat_title'].'</option>';
    }
    echo $option;
}

function getGeographic($geo='bac') {
    global $db;
    if(isset($_REQUEST['geo'])) {
        $geo = $_REQUEST['geo'];
    }
    if($geo=='bac') {
        $geo_parent = 1;
    }elseif($geo=='trung') {
        $geo_parent = 2;
    }elseif($geo=='nam') {
        $geo_parent = 3;
    }else {
        if(isset($_REQUEST['geo'])) {
            $geo_parent = intval($_REQUEST['geo']);
        }else {
            $geo_parent = $geo;
        }
    }
    $rsGeoProperty = $db->query("SELECT geo_id,geo_title FROM geographic WHERE geo_parent=$geo_parent ORDER BY geo_order");
    $option = '<option value="0">Select geographic...</option>';
    while($rs=$db->fetch_array($rsGeoProperty)) {
        $option .= '<option value="'.$rs['geo_id'].'">'.$rs['geo_title'].'</option>';
    }
    //echo $geo_parent;
    echo $option;
}
//end category function

function getGeoPropertyIdToArray($parent_id = 0,$trees=null) {
    global $db;
    if(!$trees) $trees = array();
    $rsGeo = $db->query("SELECT geo_id FROM geographic WHERE  geo_parent = ".intval($parent_id));
    if($rsGeo) {
        while($rs = $db->fetch_array($rsGeo)) {
            $trees[] = $rs['geo_id'];
            $trees = getGeoPropertyIdToArray($rs['geo_id'],$trees);
        }
    }
    return $trees;
}

function listAllPropertyGeographic($parent_id = 0,$space = ' ', $trees = NULL) {
    global $db;
    if(!$trees) $trees = array();
    $rsCat = $db->query("SELECT * FROM geographic WHERE  geo_parent = ".intval($parent_id)." ORDER BY geo_order,geo_id");
    while($rs=$db->fetch_array($rsCat)) {
        if($rs['geo_parent']==0) {
            $trees[] = array("geo_id"=>$rs['geo_id'],"geo_title"=>$rs['geo_title'],"geo_active"=>$rs['geo_active'],"create_date"=>$rs['create_date']);
        }else {
            $trees[] = array("geo_id"=>$rs['geo_id'],"geo_title"=>$space.$rs['geo_title'],"geo_active"=>$rs['geo_active'],"create_date"=>$rs['create_date']);
        }
        $trees = listAllPropertyGeographic($rs['geo_id'],$space.'----',$trees);
    }
    return $trees;
}
//start property function

function getCatIdByCatLink($catLink='') {
    global $db;
    if($catLink) {
        $rs = $db->query_first("SELECT cat_id FROM cat_property WHERE cat_link = '".mysql_real_escape_string($catLink)."'");
        if($rs['cat_id']) {
            return $rs['cat_id'];
        }else {
            return false;
        }
    }else {
        return false;
    }
}

function getGeoIdByGeoLink($geoLink='') {
    global $db;
    if($geoLink) {
        $rs = $db->query_first("SELECT geo_id FROM geographic WHERE geo_link = '".mysql_real_escape_string($geoLink)."'");
        if($rs['geo_id']) {
            return $rs['geo_id'];
        }else {
            return false;
        }
    }else {
        return false;
    }
}

function listAllPropertyByCat($catLink) {
    global $db,$param,$CONFIG;

    if(intval($param[2])>1) {
        $page = $param[2];
    }else {
        $page = 1;
    }

    $catId = getCatIdByCatLink($catLink);
    if($catId==false) {
        if($_REQUEST['type']=='2'){
            $catId = 2;
        }else{
            $catId = 1;
        }
    }
    $arrCatInfo = getCatInfoByCatId($catId);
    $catLink = $arrCatInfo['cat_link'];

    //get child category
    $arrCatId = getCatPropertyIdToArray($catId);
    array_push($arrCatId,$catId);
    if(count($arrCatId)>0) {
        $strCatId = implode(',', $arrCatId);
        $condition .= " AND p.cat_id IN ($strCatId)";
    }else {
        $condition .= " AND p.cat_id =".$catId;
    }
    //end get child category

 //search
    if(isset($_REQUEST['keyword']) && $_REQUEST['keyword']!='' && $_REQUEST['keyword']!='Tìm kiếm BĐS') {
        $keyword = $_REQUEST['keyword'];
        $keyword = str_replace(array('đ','Đ','d','D'), '_', $keyword);
        $condition .= " AND p.title LIKE '%$keyword%'
                        OR p.description LIKE '%$keyword%' ";
    }
    if(isset($_REQUEST['type'])&($_REQUEST['type']=='1'||$_REQUEST['type']=='2')) {
        if($_REQUEST['type']=='2'){
            $type = 'rent';
        }else{
            $type = 'sell';
        }
        $condition .= " AND p.type='$type' ";
    }
    if(isset($_REQUEST['cat_id']) && intval($_REQUEST['cat_id'])>0) {
        $cat_id = intval($_REQUEST['cat_id']);
        $condition .= " AND p.cat_id = $cat_id ";
    }
    if(isset($_REQUEST['price_from']) && intval($_REQUEST['price_from'])>0) {
        $price = intval($_REQUEST['price_from']);
        $condition .= " AND p.price > $price ";
    }
    if(isset($_REQUEST['price_to']) && intval($_REQUEST['price_to'])>0) {
        $price = intval($_REQUEST['price_to']);
        $condition .= " AND p.price < $price ";
    }
    if(isset($_REQUEST['price_home'])) {
        switch ($_REQUEST['price_home']) {
            case 'p500':
                $condition .= " AND (p.price*p.area_build) < 500 ";
                break;
            case 'p500-1':
                $condition .= " AND (p.price*p.area_build) >= 500 AND (p.price*p.area_build) <= 1000 ";
                break;
            case 'p1-3':
                $condition .= " AND (p.price*p.area_build) >= 1000 AND (p.price*p.area_build) <= 3000 ";
                break;
            case 'p3-5':
                $condition .= " AND (p.price*p.area_build) >= 3000 AND (p.price*p.area_build) <= 5000 ";
                break;
            case 'p5':
                $condition .= " AND (p.price*p.area_build) > 5000 ";
                break;
            case 'noPrice':
                $condition .= "";
           
                break;
        }
    }
    if(isset($_REQUEST['area'])) {
        $areaUse = $_REQUEST['area'];
        switch ($areaUse) {
            case 'a50':
                $condition .= " AND (p.area_use < '50') ";
                break;
            case 'a50-100':
                $condition .= " AND (p.area_use >= '50' AND p.area_use <= '100') ";
                break;
            case 'a101-200':
                $condition .= " AND (p.area_use >= '101' AND p.area_use <= '200') ";
                break;
            case 'a201':
                $condition .= " AND (p.area_use > '200') ";
                break;
        }
    }

    if(isset($_REQUEST['direction']) && intval($_REQUEST['direction'])>0) {
        $direction = intval($_REQUEST['direction']);
        $condition .= " AND p.direction = $direction ";
    }
    if(isset($_REQUEST['district']) && intval($_REQUEST['district'])>0) {
        $district = intval($_REQUEST['district']);
        $condition .= " AND p.geo_id=$district ";
    }else {
        if(isset($_REQUEST['city']) && intval($_REQUEST['city'])>0) {
            $city = intval($_REQUEST['city']);
            $arrGeoId = getGeoPropertyIdToArray($city);
            $strGeoId = implode(',', $arrGeoId);
            $condition .= " AND p.geo_id IN ($strGeoId) ";
        }
    }
    $condition .= " AND (p.status='actived' or p.status='closed')";
    //end search

    //order property by price
    if(isset($_POST['orderPrice'])) {
        $_SESSION['orderPrice'] = $_POST['orderPrice'];
    }
    if(isset($_SESSION['orderPrice'])) {
        if($_SESSION['orderPrice']=='1') {
            $orderSqlString = ' ORDER BY (p.price*p.area_build) ASC ';
        }elseif($_SESSION['orderPrice']=='2') {
            $orderSqlString = ' ORDER BY (p.price*p.area_build) DESC ';
        }else {
            $orderSqlString = ' ORDER BY p.order_num ';
        }
    }else {
        $orderSqlString = ' ORDER BY p.order_num ';
    }
    //end order property by price

    $rsPropertyTotal = $db->query_first("SELECT count(p.id) as total
                            FROM property p
                            LEFT JOIN cat_property c ON p.cat_id = c.cat_id
                            WHERE 1 $condition AND p.lang='".$_SESSION["lang"]."'");
    $totalRows =  $rsPropertyTotal['total'];
    $totalPages = ceil($totalRows/$CONFIG ['rows_per_page_cat_property']);
    $start = (($page*$CONFIG ['rows_per_page_cat_property'])-$CONFIG ['rows_per_page_cat_property']);
    $limit = $CONFIG ['rows_per_page_cat_property'];
$sql = "SELECT p.id,p.cat_id,p.geo_id,p.title,p.thumb,p.title_link,p.description,p.price,p.area_build,p.create_date,p.type,c.cat_title,c.cat_link
                            FROM property p LEFT JOIN cat_property c ON p.cat_id = c.cat_id
                            WHERE 1 $condition AND p.lang='".$_SESSION["lang"]."' $orderSqlString LIMIT $start,$limit";

    $rs = $db->query($sql);
    return array(
    "rs"=>$rs,
    "catLink"=>$catLink,
    "catTitle"=>$arrCatInfo['cat_title'],
    "page"=>$page,
    "totalPages"=>$totalPages,
     "totalProperty"=>$totalRows
    );

}
function listAllPropertyByGeo($geoLink) {
    global $db,$param,$CONFIG;

    if(intval($param[2])>1) {
        $page = $param[2];
    }else {
        $page = 1;
    }

    $catId = getGeoIdByGeoLink($geoLink);
    if($catId==false) {
        $catId = 1;
    }
    $arrGeoInfo = getGeoInfoByGeoId($catId);
    $geoLink = $arrGeoInfo['geo_link'];

    //get child category
    $arrCatId = getGeoPropertyIdToArray($catId);
    array_push($arrCatId,$catId);
    if(count($arrCatId)>0) {
        $strCatId = implode(',', $arrCatId);
        $condition .= " AND p.geo_id IN ($strCatId)";
    }else {
        $condition .= " AND p.geo_id =".$catId;
    }
    //end get child category
    //search
    if(isset($_REQUEST['keyword']) && $_REQUEST['keyword']!='' && $_REQUEST['keyword']!='Tìm kiếm BĐS') {
        $keyword = $_REQUEST['keyword'];
        $keyword = str_replace(array('đ','Đ','d','D'), '_', $keyword);
        $condition .= " AND p.title LIKE '%$keyword%'
                        OR p.description LIKE '%$keyword%' ";
    }
    if(isset($_REQUEST['type'])&($_REQUEST['type']=='sell'||$_REQUEST['type']=='rent')) {
        $type = $_REQUEST['type'];
        //echo $condition .= " AND p.type='$type' ";
    }
    if(isset($_REQUEST['cat_id']) && intval($_REQUEST['cat_id'])>0) {
        $cat_id = intval($_REQUEST['cat_id']);
        $condition .= " AND p.cat_id = $cat_id ";
    }
    if(isset($_REQUEST['price_from']) && intval($_REQUEST['price_from'])>0) {
        $price = intval($_REQUEST['price_from']);
        $condition .= " AND p.price > $price ";
    }
    if(isset($_REQUEST['price_to']) && intval($_REQUEST['price_to'])>0) {
        $price = intval($_REQUEST['price_to']);
        $condition .= " AND p.price < $price ";
    }
    if(isset($_REQUEST['price_home']) && intval($_REQUEST['price_home'])>0) {
        switch ($_REQUEST['price_home']) {
             case 'p500':
                $condition .= " AND (p.price*p.area_build) < 500 ";
                break;
            case 'p500-1':
                $condition .= " AND (p.price*p.area_build) >= 500 AND (p.price*p.area_build) <= 1000 ";
                break;
            case 'p1-3':
                $condition .= " AND (p.price*p.area_build) >= 1000 AND (p.price*p.area_build) <= 3000 ";
                break;
            case 'p3-5':
                $condition .= " AND (p.price*p.area_build) >= 3000 AND (p.price*p.area_build) <= 5000 ";
                break;
            case 'p5':
                $condition .= " AND (p.price*p.area_build) > 5000 ";
                break;
            case 'noPrice':
                $condition .= "";
                break;
        }
    }
    if(isset($_REQUEST['area'])) {
        $areaUse = $_REQUEST['area'];
        switch ($areaUse) {
            case 'a50':
                $condition .= " AND (p.area_use < '50') ";
                break;
            case 'a50-100':
                $condition .= " AND (p.area_use >= '50' AND p.area_use <= '100') ";
                break;
            case 'a101-200':
                $condition .= " AND (p.area_use >= '101' AND p.area_use <= '200') ";
                break;
            case 'a201':
                $condition .= " AND (p.area_use > '200') ";
                break;
        }
    }

    if(isset($_REQUEST['direction']) && intval($_REQUEST['direction'])>0) {
        $direction = intval($_REQUEST['direction']);
        $condition .= " AND p.direction = $direction ";
    }
    if(isset($_REQUEST['district']) && intval($_REQUEST['district'])>0) {
        $district = intval($_REQUEST['district']);
        $condition .= " AND p.geo_id=$district ";
    }else {
        if(isset($_REQUEST['city']) && intval($_REQUEST['city'])>0) {
            $city = intval($_REQUEST['city']);
            $arrGeoId = getGeoPropertyIdToArray($city);
            $strGeoId = implode(',', $arrGeoId);
            $condition .= " AND p.geo_id IN ($strGeoId) ";
        }
    }
    $condition .= " AND (p.status='actived' or p.status='closed')";
    //end search

    //order property by price
    if(isset($_POST['orderPrice'])) {
        $_SESSION['orderPrice'] = $_POST['orderPrice'];
    }
    if(isset($_SESSION['orderPrice'])) {
        if($_SESSION['orderPrice']=='1') {
            $orderSqlString = ' ORDER BY (p.price*p.area_build) ASC ';
        }elseif($_SESSION['orderPrice']=='2') {
            $orderSqlString = ' ORDER BY (p.price*p.area_build) DESC ';
        }else {
            $orderSqlString = ' ORDER BY p.order_num ';
        }
    }else {
        $orderSqlString = ' ORDER BY p.order_num ';
    }

    //end order property by price

    $rsPropertyTotal = $db->query_first("SELECT count(p.id) as total
                            FROM property p
                            LEFT JOIN cat_property c ON p.cat_id = c.cat_id
                            WHERE 1 $condition AND p.lang='".$_SESSION["lang"]."'");
    $totalRows =  $rsPropertyTotal['total'];
    $totalPages = ceil($totalRows/$CONFIG ['rows_per_page_cat_property']);
    $start = (($page*$CONFIG ['rows_per_page_cat_property'])-$CONFIG ['rows_per_page_cat_property']);
    $limit = $CONFIG ['rows_per_page_cat_property'];
    
    $rs = $db->query("SELECT p.id,p.cat_id,p.geo_id,p.title,p.thumb,p.title_link,p.description,p.price,p.area_build,p.create_date,p.type,c.cat_title,c.cat_link
                            FROM property p LEFT JOIN cat_property c ON p.cat_id = c.cat_id
                            WHERE 1 $condition AND p.lang='".$_SESSION["lang"]."' $orderSqlString LIMIT $start,$limit");
    return array(
    "rs"=>$rs,
    "catLink"=>$geoLink,
    "catTitle"=>$arrGeoInfo['geo_title'],
    "page"=>$page,
    "totalPages"=>$totalPages,
    "totalProperty"=>$totalRows
    );

}

function listPropertyByTime() {
    global $db,$param,$CONFIG;

    if(intval($param[2])>1) {
        $page = $param[2];
    }else {
        $page = 1;
    }

    if(intval($param[1])==0 && $param[1]=='today') {
        $catTitle = ' ngày hôm nay.';
        $timeCondition = date('Y-m-d');
        $condition.=" AND p.create_date > '$timeCondition 00:00:00' AND p.create_date < '$timeCondition 23:59:59' ";
    }elseif(intval($param[1])>0) {
        $catTitle = " $param[1] ngày truớc.";
        $currentTimeCondition = date('Y-m-d');
        $timeCondition = date_process(date('Y-m-d'),'-',intval($param[1]),'Y-m-d');
        $condition.=" AND p.create_date > '$timeCondition 00:00:00' AND p.create_date < '$currentTimeCondition 23:59:59' ";
    }

    //order property by price
    if(isset($_POST['orderPrice'])) {
        $_SESSION['orderPrice'] = $_POST['orderPrice'];
    }
    if(isset($_SESSION['orderPrice'])) {
        if($_SESSION['orderPrice']=='1') {
            $orderSqlString = ' ORDER BY p.price ASC ';
        }elseif($_SESSION['orderPrice']=='2') {
            $orderSqlString = ' ORDER BY p.price DESC ';
        }else {
            $orderSqlString = ' ORDER BY p.create_date DESC ';
        }
    }else {
        $orderSqlString = ' ORDER BY p.create_date DESC ';
    }

    if($_SESSION['property_type']=='rent') {
        $pathTitle = 'BĐS cho thuê';
        $condition .= " AND p.type='rent' ";
    }else {
        $pathTitle = 'BĐS bán';
        $condition .= " AND p.type='sell' ";
    }

    $rsPropertyTotal = $db->query_first("SELECT count(p.id) as total
                            FROM property p
                            LEFT JOIN cat_property c ON p.cat_id = c.cat_id
                            WHERE 1 $condition");
    $totalRows =  $rsPropertyTotal['total'];
    $totalPages = ceil($totalRows/$CONFIG ['rows_per_page_cat_property']);
    $start = (($page*$CONFIG ['rows_per_page_cat_property'])-$CONFIG ['rows_per_page_cat_property']);
    $limit = $CONFIG ['rows_per_page_cat_property'];

    $rs = $db->query("SELECT p.id,p.cat_id,p.geo_id,p.title,p.thumb,p.title_link,p.description,p.price,p.create_date,p.type,c.cat_title,c.cat_link
                            FROM property p LEFT JOIN cat_property c ON p.cat_id = c.cat_id
                            WHERE 1 $condition $orderSqlString LIMIT $start,$limit");
    return array(
    "rs"=>$rs,
    "pathTitle"=>$pathTitle,
    "catTitle"=>$catTitle,
    "page"=>$page,
    "totalPages"=>$totalPages,
    "totalProperty"=>$totalRows
    );

}

function getCatInfoByCatId($catId) {
    global $db;
    $rs = $db->query_first("SELECT * FROM cat_property WHERE 1 AND cat_id=$catId");
    return $rs;
}
function getGeoInfoByGeoId($geoId) {
    global $db;
    $rs = $db->query_first("SELECT * FROM geographic WHERE 1 AND geo_id=$geoId");
    return $rs;
}

function listAllProperty() {
    global $db,$CONFIG;
    if(intval($_REQUEST['page'])>0) {
        $page = $_REQUEST['page'];
    }else {
        $page = 1;
    }
    // neu' bam vao nut tim kiem
    if($_REQUEST['submitFrmPropertySearch']=='Search') {
        if($_REQUEST['keyword']!='') {
            $searchCondition = ' AND (n.title LIKE \'%'.$_REQUEST['keyword'].'%\' OR n.description LIKE \'%'.$_REQUEST['keyword'].'%\' OR n.content LIKE \'%'.$_REQUEST['keyword'].'%\')';
         
        }
        if(intval($_REQUEST['cat_id'])>0) {
            $arrCatId = getCatPropertyIdToArray($_REQUEST['cat_id']);
            array_push($arrCatId,$_REQUEST['cat_id']);
            if(count($arrCatId)>0) {
                $strCatId = implode(',', $arrCatId);
                $searchCondition .= " AND c.cat_id IN ($strCatId)";
            }else {
                $searchCondition .= " AND c.cat_id =".$_REQUEST['cat_id'];
            }
        }
        //echo $searchCondition;
        $rsProperty = $db->query("SELECT n.id FROM property n LEFT JOIN cat_property c ON n.cat_id = c.cat_id WHERE 1 $searchCondition AND n.lang='".$_SESSION['lang']."' ORDER BY n.create_date DESC");
        $totalRows =  $db->num_rows($rsProperty);
        $totalPages = ceil($totalRows/$CONFIG ['rows_per_page_property']);
        $start = (($page*$CONFIG ['rows_per_page_property'])-$CONFIG ['rows_per_page_property']);
        $limit = $CONFIG ['rows_per_page_property'];

        $rs = $db->query("SELECT n.id,n.cat_id,n.title,n.title_link,n.description,n.content,n.create_date,n.type,n.active,c.cat_title
                            FROM property n LEFT JOIN cat_property c ON n.cat_id = c.cat_id
                            WHERE 1 $searchCondition AND n.lang='".$_SESSION['lang']."' ORDER BY n.order_num LIMIT $start,$limit");
        //chuyen ve` trang truoc neu' trang hien tai khong co' ban ghi nao (truong hop xoa)
        if($db->num_rows($rs)<1) {
            $page = $page - 1;
            if($page>1) {
                server_redirect(get_string_url()."&page=".$page);
            }

        }
        return array(
        "rs"=>$rs,
        "page"=>$page,
        "totalPages"=>$totalPages
        );
    }else {
        $rsProperty = $db->query("SELECT n.id FROM property n LEFT JOIN cat_property c ON n.cat_id = c.cat_id WHERE 1 ORDER BY n.create_date DESC");
        $totalRows =  $db->num_rows($rsProperty);
        $totalPages = ceil($totalRows/$CONFIG ['rows_per_page_property']);
        $start = (($page*$CONFIG ['rows_per_page_property'])-$CONFIG ['rows_per_page_property']);
        $limit = $CONFIG ['rows_per_page_property'];

        $rs = $db->query("SELECT n.id,n.cat_id,n.title,n.title_link,n.description,n.content,n.create_date,n.type,n.active,c.cat_title
                            FROM property n LEFT JOIN cat_property c ON n.cat_id = c.cat_id
                            WHERE 1 ORDER BY n.order_num DESC LIMIT $start,$limit");
        //chuyen ve` truong truoc neu' trang hien tai khong co' ban ghi nao (truong hop xoa)
        if($db->num_rows($rs)<1) {
            $page = $page - 1;
            if($page>1) {
                redirect(get_string_url()."&page=".$page);
            }

        }
        return array(
        "rs"=>$rs,
        "page"=>$page,
        "totalPages"=>$totalPages
        );
    }
}
function show_direction($direction_number) {
    switch ($direction_number) {
        case 1:
            return 'Bắc';
            break;
        case 2:
            return 'Đông';
            break;
        case 3:
            return 'Đông bắc';
            break;
        case 4:
            return 'Đông nam';
            break;
        case 5:
            return 'Nam';
            break;
        case 6:
            return 'Tây';
            break;
        case 7:
            return 'Tây bắc';
            break;
        case 8:
            return 'Tây nam';
            break;
        default:
            break;
    }
}
/*
 * Author: VUONG VAN BINH
 * Email : wanbin@vietnambiz.com
 */
// Get all property and paging
function get_all_property($condition="",$page=1) {

    global $db,$CONFIG;

    $sel_all=$db->query_first("SELECT COUNT(id) m FROM property WHERE 1 ".$condition);

    $page=intval($page) ? intval($page) :1;
    $page=intval($page);
    if ($page<0) $page=1;
    $number=$sel_all['m'];
    $limit = $CONFIG['rows_per_page_detail_propery'];
    $total=ceil($number/$limit);
    $start = (($page*$limit)-$limit);

    $property = $db->query("SELECT title,title_link,thumb,price,cat_id FROM property WHERE 1 ".$condition." ORDER BY id DESC LIMIT ".$start.",".$limit);

    return array("property"=>$property,"page"=>$page,"total"=>$total);

}
function get_cat_property($id) {

    global $db;

    $result = $db->query_first("SELECT cat_title FROM cat_property WHERE cat_id = ".intval($id));

    return $result["cat_title"];
}
//Check cat is rent
function cat_is_rent($cat_link) {
    global $db;
    $result = $db->query_first("SELECT cat_parent FROM cat_property WHERE cat_link = '".mysql_real_escape_string($cat_link)."'");
    if($result["cat_parent"]==0) {
        $rs = $db->query_first("SELECT cat_id FROM cat_property WHERE cat_link = '".mysql_real_escape_string($cat_link)."'");
        if($rs["cat_id"]==2) {
            return true;
        }
    }else {
        if($result["cat_parent"]==2) {
            return true;
        }
    }
    return false;
}
//Check property is rent
function property_is_rent($title_link) {

    global $db;

    $result = $db->query_first("SELECT type FROM property WHERE title_link = '".mysql_real_escape_string($title_link)."'");

    if($result["type"]=='rent') {
        return true;
    }

    return false;
}
//Get property by title_link
function get_property_by_link($title_link) {

    global $db;

    $result = $db->query_first("SELECT p.id,p.cat_id,p.title,p.description,p.thumb,p.create_date,p.type,p.status,p.price,p.view_count,p.legal_doc,p.direction,p.way,p.area_campus_width as w,p.area_campus_depth as d,p.area_build,p.area_use,p.room,p.bathroom,p.floor,p.geo_id,p.user_id,p.map,c.cat_title
                            FROM property p LEFT JOIN cat_property c ON p.cat_id = c.cat_id
                            WHERE p.title_link = '".mysql_real_escape_string($title_link)."'");

    if($result) {
        return $result;
    }

//server_redirect(SITE_URL);
}
//Get images property by title_link
function get_property_images($id) {

    global $db;

    $sql = "SELECT image FROM property_image WHERE property_id=".intval($id);

    $qr=$db->query($sql);

    return $qr;

}
function get_property_contact($id) {

    global $db;
    $sql = "SELECT * FROM property_contact WHERE property_id=".intval($id);

    $qr=$db->query_first($sql);

    return $qr;

}
//GET member all info
function get_member_info($id) {

    global $db;
    $result = $db->query_first("SELECT mb.*,mbinfo.* FROM members as mb LEFT JOIN members_info as mbinfo ON mb.mem_id = mbinfo.mem_id WHERE mb.mem_id =".intval($id));

    if($result) {
        return $result;
    }

//server_redirect(SITE_URL);

}
// Get city and district by geo_id of property
function get_arr_city($id) {

    global $db;

    $arr = array();

    $result = $db->query_first("SELECT geo_parent,geo_title FROM geographic WHERE geo_id =".intval($id));

    if($result) {

        $result2 = $db->query_first("SELECT geo_title FROM geographic WHERE geo_id =".intval($result["geo_parent"]));

        $arr["city"] 	 = $result2["geo_title"];
        $arr["district"] = $result["geo_title"];

        return $arr;
    }

//server_redirect(SITE_URL);

}
//Get price trieu hoac ty


function getAllCity() {
    global $db;
    $sql="SELECT geo_id,geo_title,geo_link FROM geographic WHERE geo_link IN ('Ha-Noi','TP-Ho-chi-minh','Da-Nang') AND (geo_parent=1 OR geo_parent=2 OR geo_parent=3)
          UNION
          SELECT p.* FROM (SELECT geo_id,geo_title,geo_link FROM  geographic WHERE  geo_link NOT IN ('Ha-Noi','TP-Ho-chi-minh','Da-Nang') AND (geo_parent=1 OR geo_parent=2 OR geo_parent=3) ORDER BY  geo_order ASC) AS p";
    return $db->query($sql);
}
function getAllCityByParent($parent) {
    global $db;
    $sql="SELECT geo_id,geo_title,geo_link  FROM geographic WHERE geo_link IN ('Ha-Noi','TP-Ho-chi-minh','Da-Nang') AND (geo_parent=$parent)
          UNION
          SELECT p.* FROM (SELECT geo_id,geo_title,geo_link  FROM  geographic WHERE  geo_link NOT IN ('Ha-Noi','TP-Ho-chi-minh','Da-Nang') AND (geo_parent=$parent) ORDER BY  geo_id ASC) AS p";
    return $db->query($sql);
}
function getDistrictByCityId($cityId) {
    global $db;
    $sql="SELECT geo_id,geo_title FROM geographic WHERE geo_parent=".intval($cityId);
    return $db->query($sql);
}


function getCatInfoBylink($catLink) {
    global $db;
    $catId = getCatIdByCatLink($catLink);
    if($catId==false) {
        $catId = 1;
    }
    $rs = $db->query_first("SELECT * FROM cat_property WHERE 1 AND cat_id=$catId");
    return $rs;
}

function getGeoInfoBylink($geoLink) {
    global $db;
    $geoId = getGeoIdByGeoLink($geoLink);
    if($geoId==false) {
        $geoId = 1;
    }
    $rs = $db->query_first("SELECT * FROM geographic WHERE 1 AND geo_id=$geoId");
    return $rs;
}

function listPropertyCategoryByParent($parent_id = 0,$space = ' ', $trees = NULL) {
    global $db;
    if(!$trees) $trees = array();
    $rsCat = $db->query("SELECT * FROM cat_property WHERE  cat_parent = ".intval($parent_id)." AND lang='".$_SESSION["lang"]."' ORDER BY cat_order,cat_id");
    while($rs=$db->fetch_array($rsCat)) {
        if($rs['cat_parent']==0) {
            $trees[] = array("cat_id"=>$rs['cat_id'],"cat_title"=>$rs['cat_title'],"cat_active"=>$rs['cat_active'],"create_date"=>$rs['create_date']);
        }else {
            $trees[] = array("cat_id"=>$rs['cat_id'],"cat_title"=>$space.$rs['cat_title'],"cat_active"=>$rs['cat_active'],"create_date"=>$rs['create_date']);
        }
    }
    return $trees;
}
?>
